package dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import beans.Alternativa;

public class AlternativaDAO {
	
	private DatabaseHelper dbHelper;
	private SQLiteDatabase dbWrite;
	private SQLiteDatabase dbRead;
	
	public AlternativaDAO(Context context){
		this.dbHelper = new DatabaseHelper(context);
		this.dbWrite = this.dbHelper.getWritableDatabase();
		this.dbRead = this.dbHelper.getReadableDatabase();
	}

	public long inserir (Alternativa alternativa, long questao_id){
		ContentValues values = new ContentValues();
	    values.put(this.dbHelper.getKey_alternativa_descricao(), alternativa.getDescricao());
	    values.put(this.dbHelper.getKey_alternativa_correta(), alternativa.getCorreta());
	    values.put(this.dbHelper.getKey_alternativa_questao_id(), questao_id);

	    // insert row
	    long alternativa_id = dbWrite.insert(this.dbHelper.getTable_alternativa(), null, values);
	    
	    return alternativa_id;
	}
	
	public int atualizar (Alternativa alternativa, long questao_id){
		ContentValues values = new ContentValues();
		values.put(this.dbHelper.getKey_alternativa_descricao(), alternativa.getDescricao());
	    values.put(this.dbHelper.getKey_alternativa_correta(), alternativa.getCorreta());
	    values.put(this.dbHelper.getKey_alternativa_questao_id(), questao_id);

	    // update row
	    int retorno = dbWrite.update(this.dbHelper.getTable_questao(), values,
	    		this.dbHelper.getKey_id() + " = ?", 
	    		new String[] { String.valueOf(alternativa.getId()) });
	 
	    return retorno;
	}
	
	public int deletar (int id){
		return 1;
	}
	
	public Alternativa obter (int id){
		 
	    String selectQuery = "SELECT  * FROM " + this.dbHelper.getTable_alternativa() + " WHERE "
	            + this.dbHelper.getKey_id() + " = " + id;
	 
	    Cursor c = dbRead.rawQuery(selectQuery, null);
	 
	    if (c != null){
	        c.moveToFirst();
	    }
	    Alternativa alternativa = new Alternativa();
	    alternativa.setId(c.getInt(c.getColumnIndex(this.dbHelper.getKey_id())));
	    alternativa.setDescricao((c.getString(c.getColumnIndex(this.dbHelper.getKey_alternativa_descricao()))));
	    alternativa.setCorreta(c.getInt(c.getColumnIndex(this.dbHelper.getKey_alternativa_correta())));
	    alternativa.setQuestao_id(c.getLong(c.getColumnIndex(this.dbHelper.getKey_alternativa_questao_id())));
	 
	    return alternativa;
	}
	
	public List<Alternativa> listaAlternativas(long id){
		
		List<Alternativa> alternativas = new ArrayList<Alternativa>();
	    String selectQuery = "SELECT  * FROM " + this.dbHelper.getTable_alternativa() + " WHERE "
	            + this.dbHelper.getKey_alternativa_questao_id() + " = " + id;
	 
	    Cursor c = dbRead.rawQuery(selectQuery, null);
	 
	 // looping through all rows and adding to list
	    if (c.moveToFirst()) {
	        do {

	        	Alternativa alternativa = new Alternativa();
	        	alternativa.setId(c.getInt(c.getColumnIndex(this.dbHelper.getKey_id())));
	        	alternativa.setDescricao((c.getString(c.getColumnIndex(this.dbHelper.getKey_alternativa_descricao()))));
	        	alternativa.setCorreta(c.getInt(c.getColumnIndex(this.dbHelper.getKey_alternativa_correta())));
	        	alternativa.setQuestao_id(c.getLong(c.getColumnIndex(this.dbHelper.getKey_alternativa_questao_id())));
	 

	            // adding to questaos list
	        	alternativas.add(alternativa);

			} while (c.moveToNext());
		}

	    return alternativas;
	}
}
